﻿Imports System.IO
Namespace helper
    ''' <summary>
    ''' provides methos due with csv file
    ''' </summary>
    ''' <remarks></remarks>
    Public NotInheritable Class csvHelper


        ''' <summary>
        ''' Read data from a csv file
        ''' </summary>
        ''' <param name="path">file path</param>
        ''' <returns>retuen datatable</returns>
        ''' <remarks></remarks>
        Public Shared Function Read(ByVal path As String) As DataTable
            'validation
            If String.IsNullOrEmpty(path) Then
                Throw New ArgumentNullException("path")
            End If


            Try
                Dim sr As New StreamReader(path)
                Dim fullFileStr As String = sr.ReadToEnd()
                sr.Close()
                sr.Dispose()
                Dim lines As String() = fullFileStr.Split(ControlChars.Lf)
                Dim recs As New DataTable()
                Dim sArr As String() = lines(0).Split(","c)
                For Each s As String In sArr
                    recs.Columns.Add(New DataColumn())
                Next
                Dim row As DataRow
                Dim finalLine As String = ""
                For Each line As String In lines
                    row = recs.NewRow()
                    finalLine = line.Replace(Convert.ToString(ControlChars.Cr), "")
                    row.ItemArray = finalLine.Split(","c)
                    recs.Rows.Add(row)
                Next
                Return recs
            Catch ex As Exception
                Throw ex
            End Try
        End Function
    End Class
End Namespace

